package com.xunwulian.web.util;

import com.xunwulian.base.entity.Menu;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import java.util.List;
import java.util.Map;

/**
 * 权限处理
 *
 * @author:fh
 */
public class JurisdictionHelper {

    /**
     * 访问权限及初始化按钮权限(控制按钮的显示)
     *
     * @param paramMenuUrl 菜单路径
     * @return
     */
    public static boolean hasJurisdiction(String paramMenuUrl,
                                          String userName,
                                          List<Menu> menuList,
                                          Map<String, String> mapQX) {

        //判断是否拥有当前点击菜单的权限（内部过滤,防止通过url进入跳过菜单权限）
        /**
         * 根据点击的菜单的xxx.do去菜单中的URL去匹配，当匹配到了此菜单，判断是否有此菜单的权限，没有的话跳转到404页面
         * 根据按钮权限，授权按钮(当前点的菜单和角色中各按钮的权限匹对)
         */

        for (int i = 0; i < menuList.size(); i++) {
            for (int j = 0; j < menuList.get(i).getSubMenu().size(); j++) {

                Menu menu = menuList.get(i).getSubMenu().get(j);

                if (menu.getMENU_URL().split(".do")[0].equals(paramMenuUrl.split(".do")[0])) {
                    if (!menu.isHasMenu()) {                //判断有无此菜单权限
                        return false;
                    }

                    //按钮判断
                    Integer menuId = menu.getMENU_ID();

                    if ("admin".equals(userName)) {
                        mapQX.put("add", "1");
                        mapQX.put("del", "1");
                        mapQX.put("edit", "1");
                        mapQX.put("cha", "1");
                    } else {
                        mapQX.put("add", (RightsHelper.testRights(mapQX.get("adds"), menuId)) ? "1" : "0");
                        mapQX.put("del", RightsHelper.testRights(mapQX.get("dels"), menuId) ? "1" : "0");
                        mapQX.put("edit", RightsHelper.testRights(mapQX.get("edits"), menuId) ? "1" : "0");
                        mapQX.put("cha", RightsHelper.testRights(mapQX.get("chas"), menuId) ? "1" : "0");
                    }
                }
            }
        }
        return true;
    }

    /**
     * 判断是否拥有当前点击菜单的权限（内部过滤,防止通过url进入跳过菜单权限）
     * 根据菜单的路径xxx.do去匹配，当匹配到了此菜单，判断是否有此菜单的权限，没有的话跳转到404页面
     * 根据按钮权限，授权按钮
     */
    public static boolean buttonJurisdiction(String paramMenuUrl, String operateType) {
        // shiro管理的session
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        // 获取菜单列表
        List<Menu> menuList = (List) session.getAttribute(Const.SESSION_allmenuList);
        // 按钮权限
        Map<String, String> rightsMap = (Map<String, String>) session.getAttribute(Const.SESSION_QX);

        // 获取当前登录者loginname
        String userName = session.getAttribute(Const.SESSION_USERNAME).toString();
        Boolean isAdmin = "admin".equals(userName);

        return testRights(paramMenuUrl, operateType, menuList, rightsMap, isAdmin);
    }

    private static boolean testRights(String paramMenuUrl, String operateType, List<Menu> menuList,
                                      Map<String, String> rightsMap, boolean isAdmin) {
        for (int i = 0; i < menuList.size(); i++) {
            for (int j = 0; j < menuList.get(i).getSubMenu().size(); j++) {
                Menu menu = menuList.get(i).getSubMenu().get(j);

                if (menu.getMENU_URL().split(".do")[0].equals(paramMenuUrl.split(".do")[0])) {
                    // 判断有无此菜单权限
                    if (!menu.isHasMenu()) {
                        return false;
                    } else {
                        // 按钮判断
                        Integer menuId = menu.getMENU_ID();
                        if ("add".equals(operateType)) {
                            return ((RightsHelper.testRights(rightsMap.get("adds"), menuId)) || isAdmin);
                        } else if ("del".equals(operateType)) {
                            return ((RightsHelper.testRights(rightsMap.get("dels"), menuId)) || isAdmin);
                        } else if ("edit".equals(operateType)) {
                            return ((RightsHelper.testRights(rightsMap.get("edits"), menuId)) || isAdmin);
                        } else if ("cha".equals(operateType)) {
                            return ((RightsHelper.testRights(rightsMap.get("chas"), menuId)) || isAdmin);
                        }
                    }
                }
            }
        }
        // 默认无权限
        return true;
    }

}
